package com.imonsoft.sqm.service.business.ipic_system;

import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.exception.SystemException;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.util.DateUtil;
import com.imonsoft.common.util.GenerateFlowCode;
import com.imonsoft.sqm.dao.business.ipic_system.ClaimDAO;
import com.imonsoft.sqm.pojo.basedata.Part;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.pojo.business.ipic_system.Claim;
import com.imonsoft.sqm.pojo.business.ipic_system.ClaimExample;
import com.imonsoft.sqm.service.basedata.PartService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service("claimFieldService")
public class ClaimFieldServiceImpl implements ClaimFieldService {
    protected final Log logger = LogFactory.getLog(getClass());
    @Autowired
    private ClaimDAO claimDAO;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private PartService partService;

    @Override
    public void deleteClaim(List<Long> idList) {
        if(null==idList || idList.isEmpty()){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        for(int i=0;i<idList.size();i++){
            Claim claim=claimDAO.selectClaimByPrimaryKey(idList.get(i));
            claim.setState(ConstantSqm.STATUS_DELETE);
            claimDAO.updateClaimByPrimaryKey(claim);
        }
    }

    @Override
    public List<Claim> findClaimByCode(String code) {
        if(StringUtils.isEmpty(code)){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        ClaimExample example = new ClaimExample();
        ClaimExample.Criteria criteria = example.createCriteria();
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(!StringUtils.isEmpty(code)){
            criteria.andCodeEqualTo(code);
        }
        List<Claim> claimList=claimDAO.selectClaimByExample(example);
        return claimList;
    }

    @Override
    public String generateFlowCodeOfYear(String year) {
        ClaimExample example = new ClaimExample();
        ClaimExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(year)){
            criteria.andCodeLike("%"+year+"-___");
        }
        List<Claim> claimList=claimDAO.selectClaimByExample(example);
        if(null==claimList){
            return GenerateFlowCode.frontAddZero("1");
        }
        return GenerateFlowCode.frontAddZero("" + (claimList.size()+1));
    }

    @Override
    public Long insert(Claim entity) {
        if(null==entity){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        if(null!=entity.getSupplierId()){
            Supplier supplier=supplierService.get(entity.getSupplierId());
            entity.setSupplierCode(supplier.getCode());
            entity.setSupplierName(supplier.getName());
        }
        if(null!=entity.getPartId()){
            Part part=partService.get(entity.getPartId());
            entity.setPartCode(part.getCode());
            entity.setPartName(part.getName());
        }
        entity.setCode(entity.getCode()+generateFlowCodeOfYear(entity.getCode().substring(9,11)));
        entity.setType(ConstantSqm.TYPE_FIELD_CLAIM);
        entity.setState(ConstantSqm.STATUS_NORMAL);
        commonServe(entity);
        return claimDAO.insertClaim(entity);
    }
    private void commonServe(Claim entity){
        entity.setCloseDatePlan(DateUtil.string2date(DateUtil.getDate(entity.getStartDate(), 20)));
        if (!StringUtils.isEmpty(entity.getCs9()) && !entity.getCs9().equals(ConstantSqm.CLAIM_CS9_NA)) {
            entity.setCloseDateActual(DateUtil.string2date(entity.getCs9()));
        } else if (StringUtils.isEmpty(entity.getCs9()) || entity.getCs9().equals(ConstantSqm.CLAIM_CS9_NA)) {
            if (!StringUtils.isEmpty(entity.getCs8())) {
                entity.setCloseDateActual(entity.getCs8());
            }
        }
    }
    @Override
    public int update(Claim entity) {
        if(null==entity || null==entity.getId()){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        Claim claim=claimDAO.selectClaimByPrimaryKey(entity.getId());
        claim.setSupplierId(entity.getSupplierId());
        if(null!=entity.getSupplierId()){
            Supplier supplier=supplierService.get(entity.getSupplierId());
            claim.setSupplierCode(supplier.getCode());
            claim.setSupplierName(supplier.getName());
        }
        claim.setPartId(entity.getPartId());
        if(null!=entity.getPartId()){
            Part part=partService.get(entity.getPartId());
            claim.setPartCode(part.getCode());
            claim.setPartName(part.getName());
        }
        claim.setManufactureDate(entity.getManufactureDate());
        claim.setDeliveryBatchCode(entity.getDeliveryBatchCode());
        claim.setLotSize(entity.getLotSize());
        claim.setAmountNotQualified(entity.getAmountNotQualified());
        claim.setWorkshopCode(entity.getWorkshopCode());
        claim.setFindPlace(entity.getFindPlace());
        claim.setStartDate(entity.getStartDate());
        claim.setCloseDatePlan(entity.getCloseDatePlan());
        claim.setCloseDateActual(entity.getCloseDateActual());
        claim.setManageBody(entity.getManageBody());
        claim.setClaimStatus(entity.getClaimStatus());
        claim.setClaimType(entity.getClaimType());
//        claim.setNotQualifiedReportCode(entity.getNotQualifiedReportCode());
        claim.setProblemDesc(entity.getProblemDesc());

        claim.setCs1(entity.getCs1());
        claim.setCs2(entity.getCs2());
        claim.setCs3(entity.getCs3());
        claim.setCs4(entity.getCs4());
        claim.setCs5(entity.getCs5());
        claim.setCs6(entity.getCs6());
        claim.setCs7(entity.getCs7());
        claim.setCs8(entity.getCs8());
        claim.setCs9(entity.getCs9());

        claim.setAffectParts(entity.getAffectParts());
        claim.setComment(entity.getComment());
        claim.setCustomerClaimPart(entity.getCustomerClaimPart());
        claim.setSupplierAcceptPart(entity.getSupplierAcceptPart());
        claim.setLastYearReceivePart(entity.getLastYearReceivePart());
        claim.setPpms(entity.getPpms());
        claim.setPpmAtNumber(entity.getPpmAtNumber());
        claim.setClosed(entity.getClosed());

        if(!StringUtils.isEmpty(entity.getPicturePath())){
            claim.setPicturePath(entity.getPicturePath());
        }
        claim.setReporterCode(entity.getReporterCode());
        claim.setReporterName(entity.getReporterName());
        claim.setReporterMail(entity.getReporterMail());
        claim.setReporterPhone(entity.getReporterPhone());
        claim.setReporterFax(entity.getReporterFax());

        commonServe(claim);
        return claimDAO.updateClaimByPrimaryKey(claim);
    }

    @Override
    public int delete(Long id) {
        if(null==id){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        return claimDAO.deleteClaimByPrimaryKey(id);
    }

    @Override
    public Claim get(Long id) {
        if(null==id){
            throw new SystemException(ConstantSqm.DATA_ERROR);
        }
        Claim claim=claimDAO.selectClaimByPrimaryKey(id);
        return claim;
    }

    @Override
    public List<Claim> findList(Claim entity) {
        ClaimExample example = new ClaimExample();
        ClaimExample.Criteria criteria = example.createCriteria();
        if(null!=entity){
            if(!StringUtils.isEmpty(entity.getCode())){
                criteria.andCodeLike("%"+entity.getCode()+"%");
            }
        }
        List<Claim> claimList=claimDAO.selectClaimByExample(example);
        return claimList;
    }

    @Override
    public DataGrid<Claim> getDataGrid(Claim entity, Pager pager){
        ClaimExample example = new ClaimExample();
        example.setOrderByClause("id desc");
        ClaimExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(ConstantSqm.TYPE_FIELD_CLAIM);
        criteria.andStateNotEqualTo(ConstantSqm.STATUS_DELETE);
        if(null!=entity){
            if(!StringUtils.isEmpty(entity.getCode())){
                criteria.andCodeLike(entity.getCode() + "%");
            }
            if(!StringUtils.isEmpty(entity.getSupplierCode())){
                criteria.andSupplierCodeLike(entity.getSupplierCode() + "%");
            }
            if(!StringUtils.isEmpty(entity.getPartCode())){
                criteria.andPartCodeLike(entity.getPartCode() + "%");
            }
        }
        return claimDAO.getDataGrid(example,pager);
    }
}
